<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<style type="text/css">
		body {
			margin: 0;
			font-family: -apple-system;
			font-size: 14px;
		}
		menu {
			list-style: none;
			padding: 0;
			margin: 4px 0;
		}
		li {
			line-height: 1.5em;
			padding: 0 20px;
			white-space: nowrap;
		}
		li:hover {
			background-color: #4397fa;
			color: #fff;
		}
		a {
			cursor: default;
			color: inherit;
			text-decoration: inherit;
		}
		hr {
			border-top: 2px #ddd solid;
		}
	</style>
</head>
<body>
<menu>
	<li id="zotero-save-page-with-snapshot"><a href="javascript:void(0)">Save to Zotero (Web Page with Snapshot)</a></li>
	<li id="zotero-save-page-without-snapshot"><a href="javascript:void(0)">Save to Zotero (Web Page without Snapshot)</a></li>
	<li id="zotero-save-PDF" style="display: none"><a href="javascript:void(0)">Save to Zotero (PDF)</a></li>
	<hr/>
	<li id="zotero-preferences"><a href="javascript:void(0)">Zotero Preferences</a></li>
</menu>
<script>
	(function () {
		document.getElementById('zotero-preferences').onclick = function() {
			var Zotero_Connector_Browser = safari.extension.globalPage.contentWindow.Zotero.Connector_Browser;
			Zotero_Connector_Browser.openTab(`${safari.extension.baseURI}safari/` + "preferences/preferences.html");
			safari.self.hide();
		};
		function saveAsWebpage(snapshot=true) {
			var Zotero_Connector_Browser = safari.extension.globalPage.contentWindow.Zotero.Connector_Browser;
			Zotero_Connector_Browser.saveAsWebpage(
				Zotero_Connector_Browser.activeTab,
				{
					snapshot
				}
			);
			safari.self.hide();
		}
		var saveWithSnapshotElem = document.getElementById('zotero-save-page-with-snapshot');
		var saveWithoutSnapshotElem = document.getElementById('zotero-save-page-without-snapshot');
		var savePDFElem = document.getElementById('zotero-save-PDF');
		saveWithSnapshotElem.onclick = saveAsWebpage;
		savePDFElem.onclick = saveAsWebpage;
		saveWithoutSnapshotElem.onclick = saveAsWebpage.bind(this, [false]);

		safari.application.addEventListener('popover', function (event) {
			var Zotero = safari.extension.globalPage.contentWindow.Zotero;
			var tab = Zotero.Connector_Browser.activeTab;
			setTimeout(function () {
				safari.self.height = document.documentElement.offsetHeight;
				let width = 0;
				let aElems = document.getElementsByTagName('a');
				for (let i = 0; i < aElems.length; i++) {
					if (aElems[i].offsetWidth > width) {
						width = aElems[i].offsetWidth;
					}
				}
				safari.self.width = width + 40;
			});

			let translatorElems = document.getElementsByClassName('translator');
			for (let i = translatorElems.length-1; i >= 0; i--) {
				let li = translatorElems[i];
				li.parentNode.removeChild(li);
			}

			var withSnapshot = Zotero.Connector.isOnline ? Zotero.Connector.automaticSnapshots :
				Zotero.Prefs.get('automaticSnapshots');
			if (withSnapshot) {
				document.getElementsByTagName('menu')[0]
					.insertBefore(saveWithSnapshotElem, saveWithoutSnapshotElem);
			} else {
				document.getElementsByTagName('menu')[0]
					.insertBefore(saveWithoutSnapshotElem, saveWithSnapshotElem);
			}

			var isPDF = tab.contentType == 'application/pdf' || tab.isPDFFrame;
			saveWithoutSnapshotElem.style.display = isPDF ? 'none' : null;
			saveWithSnapshotElem.style.display = isPDF ? 'none' : null;
			savePDFElem.style.display = isPDF ? null : 'none';

			var translators = tab.translators;
			if (!translators || event.target.identifier != 'zotero-translatorSelector') return;

			var menu = document.getElementsByTagName('menu')[0];
			var savePage = menu.firstChild;
			var Zotero = safari.extension.globalPage.contentWindow.Zotero;
			for (let i = 0; i < translators.length; i++) {
				let translator = translators[i];
				let li = document.createElement('li');
				li.classList.add('translator');

				let a = document.createElement('a');
				a.setAttribute('href', 'javascript:void(0);');
				a.textContent = `${Zotero.getString('general_saveTo', 'Zotero')} (${translator.label})`;
				li.onclick = function () {
					Zotero.Connector_Browser.saveWithTranslator(
						tab,
						i
					);
					safari.self.hide();
				};

				li.appendChild(a);
				menu.insertBefore(li, savePage);
			}
		});
	})();
</script>
</body>
</html>